﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.Client.App;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Interface;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Control.DateTimeGrid;
namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class SchedulingView : BaseUserControl
    {
        private DBEntityCustomInfo customInfo = null;
        /// <summary>
        /// 单个会员预约数据集，表1存放 schedule 表2存放scheduleCustom
        /// </summary>
        private DataSet dsSchduleInfoForCustom = new DataSet();
        /// <summary>
        /// 本月预约数据集
        /// </summary>
        private DataSet dsScheduleInfoForMonth = null;

        private DataSet dsScheduleInfoForDay = null;

        public SchedulingView()
        {
            InitializeComponent();
        }

        public SchedulingView(CharmManagerApp _app)
            : base(_app)
        {
            InitializeComponent();

            #region 会员预约
            //查找控件
            customSearchSchdule.onSerchCustom = this.onSerchCustomCustom;
            //时间空间
            monthDateTimeForMonthCustomSchedule.OnEventCurrMonth = EventClickMonth;
            #endregion

            #region 本月预约
            monthDateTimeForCurrMonth.OnEventCurrMonth = OnEventCurrMonthClickMonth;
            monthDateTimeForCurrMonth.monthDateTimeForWeek.OnEventDBClickCurrDay += OnEventDBClickCurrDay;
            //monthDateTimeForCurrMonth.curr
            #endregion
        }

        #region 会员预约
        /// <summary>
        /// 点击完用户查询后的委托
        /// </summary>
        /// <param name="drCustomInfo"></param>
        private void onSerchCustomCustom(DataRow drCustomInfo)
        {
            if (drCustomInfo == null)
            {
                return;
            }
            customInfo = new DBEntityCustomInfo(drCustomInfo);
            employeeSimpleInfo.CustomInfo = customInfo;
            //触发查询
            bindQueryScheduleCustom(monthDateTimeForMonthCustomSchedule.MonthDateTime, customInfo.CUSTOMERID);

        }
        /// <summary>
        /// 会员预约页签中的添加预约信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddCustomSchedule_Click(object sender, EventArgs e)
        {
            if (customInfo == null)
            {
                MsgBox.MsgInfo("请选择要预约的会员。");
                return;
            }
            frmScheduleInfoAddCustom fromAddSchdule = new frmScheduleInfoAddCustom(null, null, customInfo);
            if (fromAddSchdule.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }

            bindQueryScheduleCustom(monthDateTimeForMonthCustomSchedule.MonthDateTime, customInfo.CUSTOMERID);

        }
        /// <summary>
        /// 点击月份切换
        /// </summary>
        /// <param name="currMonth"></param>
        private void EventClickMonth(DateTime currMonth)
        {
            if (customInfo == null)
            {
                return;
            }
            bindQueryScheduleCustom(currMonth, customInfo.CUSTOMERID);

        }
        /// <summary>
        /// 刷新控件数据
        /// </summary>
        /// <param name="callTime">当前时间</param>
        /// <param name="customID">用户ＩＤ</param>
        private void bindQueryScheduleCustom(DateTime callTime, int customID)
        {
            string error = string.Empty;
            if (!BussinessProxyManager.Instance.QueryScheduleCustomDateTime(customID, callTime, out dsSchduleInfoForCustom, out error))
            {
                MsgBox.MsgInfo(error);
                return;
            }
            monthDateTimeForMonthCustomSchedule.DataBindForMonth(dsSchduleInfoForCustom, "DISPVALUE", "SCHEDULEID", "SCALLDATE");
        }
        /// <summary>
        /// 修改预约记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEditSchduleInfo_Click(object sender, EventArgs e)
        {
            MonthDateTimeForDay dayControl = monthDateTimeForMonthCustomSchedule.CurrDateTimeViewDayControl;
            DataRow drScheduleInfo = dayControl.GetCurrDataRow();
            if (drScheduleInfo == null)
            {
                MsgBox.MsgInfo("请选择正确的预约信息。");
                return;
            }

            DBEntitySchedule scheduleInfo = new DBEntitySchedule(drScheduleInfo);
            DBEntityScheduleCustom scheduleInfoCustom = new DBEntityScheduleCustom(drScheduleInfo);
            frmScheduleInfoAddCustom fromAddSchdule = new frmScheduleInfoAddCustom(scheduleInfo, scheduleInfoCustom, customInfo);
            if (fromAddSchdule.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            bindQueryScheduleCustom(monthDateTimeForMonthCustomSchedule.MonthDateTime, customInfo.CUSTOMERID);
        }
        /// <summary>
        /// 删除预约记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteSchduleInfo_Click(object sender, EventArgs e)
        {
            if (!MsgBox.MsgQue("是否要删除所选择的预约记录？"))
            {
                return;
            }
            MonthDateTimeForDay dayControl = monthDateTimeForMonthCustomSchedule.CurrDateTimeViewDayControl;
            DataRow drScheduleInfo = dayControl.GetCurrDataRow();
            if (drScheduleInfo == null)
            {
                MsgBox.MsgInfo("请选择正确的预约信息。");
                return;
            }
            DBEntitySchedule scheduleInfo = new DBEntitySchedule(drScheduleInfo);
            DBEntityScheduleCustom scheduleInfoCustom = new DBEntityScheduleCustom(drScheduleInfo);
            string error = string.Empty;
            if (!BussinessProxyManager.Instance.DeleteScheduleCustom(scheduleInfo, scheduleInfoCustom, out error))
            {
                MsgBox.MsgInfo("删除错误：" + error);
                return;
            }
            dayControl.DelItem(dayControl.GetItemID());
        }
        /// <summary>
        /// 刷新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReferchSchedule_Click(object sender, EventArgs e)
        {
            if (customInfo == null)
            {
                return;
            }
            MonthDateTimeForDay dayControl = monthDateTimeForMonthCustomSchedule.CurrDateTimeViewDayControl;
            bindQueryScheduleCustom(dayControl.GetDateTime(), customInfo.CUSTOMERID);
        }

        #endregion

        #region 本月预约
        private void tabItemCurrSchedule_Click(object sender, EventArgs e)
        {
            if (dsScheduleInfoForMonth == null)
            {
                QueryCurrMonthSchedule(DateTime.Now);
            }
        }
        /// <summary>
        /// 双击日期控件
        /// </summary>
        /// <param name="sender"></param>
        private void OnEventDBClickCurrDay(object sender)
        {
            MonthDateTimeForDay dayControl = (MonthDateTimeForDay)sender;
            grdCtrlScheduleMonth.DataSource = dsScheduleInfoForMonth.Tables[0].DefaultView;
            //初始化下面的GRID
            dsScheduleInfoForMonth.Tables[0].DefaultView.RowFilter = "SCALLDATE > #" + dayControl.GetDateTime().ToShortDateString() + " 00:00:00" + "#"
                + " AND SCALLDATE < #" + dayControl.GetDateTime().ToShortDateString() + " 23:59:59" + "#";

        }
        /// <summary>
        /// 月份切换事件
        /// </summary>
        /// <param name="currDate"></param>
        private void OnEventCurrMonthClickMonth(DateTime currDate)
        {
            QueryCurrMonthSchedule(currDate);
            OnEventDBClickCurrDay(monthDateTimeForCurrMonth.CurrDateTimeViewDayControl);
        }

        private void QueryCurrMonthSchedule(DateTime dateMonth)
        {
            string error = string.Empty;
            if (!BussinessProxyManager.Instance.QueryScheduleMonth(dateMonth.Year, dateMonth.Month, out dsScheduleInfoForMonth, out error))
            {
                MsgBox.MsgInfo("查询本月预约错误：" + error);
                return;
            }

            monthDateTimeForCurrMonth.DataBindForMonth(dsScheduleInfoForMonth, "DISPVALUE", "SCHEDULEID", "SCALLDATE");
            
        }

        private void btnCurrScheduleEdit_Click(object sender, EventArgs e)
        {
            if (grdViewScheduleMonth.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择列表中的会员记录。");
                return;
            }
            MonthDateTimeForDay currDay = monthDateTimeForCurrMonth.CurrDateTimeViewDayControl;

            DataRow drCurrRow = grdViewScheduleMonth.GetDataRow(grdViewScheduleMonth.FocusedRowHandle);
            DBEntitySchedule scheduleInfo = new DBEntitySchedule(drCurrRow);
            DBEntityScheduleCustom scheduleInfoCustom = new DBEntityScheduleCustom(drCurrRow);
            DBEntityCustomInfo customInfo = new DBEntityCustomInfo(drCurrRow);
            frmScheduleInfoAddCustom schduleInfoFrom = new frmScheduleInfoAddCustom(scheduleInfo, scheduleInfoCustom, customInfo);

            if (schduleInfoFrom.ShowDialog() == DialogResult.OK)
            {
                //刷新界面数据
                QueryCurrMonthSchedule(currDay.GetDateTime());
                //刷新列表数据
                OnEventDBClickCurrDay(currDay);
            }
        }

        private void btnCurrScheduleDelete_Click(object sender, EventArgs e)
        {
            if (grdViewScheduleMonth.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择列表中的会员记录。");
                return;
            }
            if (!MsgBox.MsgOkCacel("是否要删除所选择的预约记录？"))
            {
                return;
            }
            string error = string.Empty;
            MonthDateTimeForDay currDay = monthDateTimeForCurrMonth.CurrDateTimeViewDayControl;
            DataRow drCurrRow = grdViewScheduleMonth.GetDataRow(grdViewScheduleMonth.FocusedRowHandle);
            DBEntitySchedule scheduleInfo = new DBEntitySchedule(drCurrRow);
            DBEntityScheduleCustom scheduleInfoCustom = new DBEntityScheduleCustom(drCurrRow);
            if (!BussinessProxyManager.Instance.DeleteScheduleCustom(scheduleInfo, scheduleInfoCustom, out error))
            {
                MsgBox.MsgInfo("删除错误：" + error);
            }
            else
            {
                MsgBox.MsgInfo("删除完毕。");
                //刷新界面数据
                QueryCurrMonthSchedule(currDay.GetDateTime());
                //刷新列表数据
                OnEventDBClickCurrDay(currDay);
            }
        }

        private void btnCurrScheduleReferch_Click(object sender, EventArgs e)
        {
            grdCtrlScheduleMonth.DataSource = null;
            QueryCurrMonthSchedule(DateTime.Now);            
            MsgBox.MsgInfo("刷新完毕。");
        }
        /// <summary>
        /// 双击修改预约记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdViewScheduleMonth_DoubleClick(object sender, EventArgs e)
        {
            btnCurrScheduleEdit_Click(sender, e);
        }

        private void grdViewScheduleMonth_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle < 0)
                return;

            if (e.Column.FieldName.Equals(DBEntityScheduleCustom.FieldName.SBESPEAKSTATE))
            {
                e.DisplayText = DBEntityScheduleCustom.ENUM_SBESPEAKSTATE.EnumStrToChineseStr(e.CellValue.ToString());
            }
        }
        #endregion

        #region 今天预约

        //gvCurrSchedule
        private void initTodaySchedule()
        {
            dsScheduleInfoForDay = new DataSet();
            string error = string.Empty;
            //if (!BussinessProxyManager.Instance.QueryScheduleDay(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, out dsScheduleInfoForDay, out error))
            if (!BussinessProxyManager.Instance.QueryScheduleMonth(DateTime.Now.Year, DateTime.Now.Month, out dsScheduleInfoForDay, out error))
            {
                MsgBox.MsgInfo("获取当前预约数据错误：" + error);
                return;
            }
            gdCurrSchedule.DataSource = dsScheduleInfoForDay.Tables[0].DefaultView;
        }

        private void gvCurrSchedule_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle < 0)
                return;

            if (e.Column.FieldName.Equals(DBEntityScheduleCustom.FieldName.SBESPEAKSTATE))
            {
                e.DisplayText = DBEntityScheduleCustom.ENUM_SBESPEAKSTATE.EnumStrToChineseStr(e.CellValue.ToString());
            }
        }

        private void QueryTodaySchedule(string QueryState)
        {
            string filter = string.Empty;
            if (QueryState.Equals("全部信息"))
            {
                filter = "1=1";
            }
            else
            {
                filter = "SBESPEAKSTATE = '" + DBEntityScheduleCustom.ENUM_SBESPEAKSTATE.ChineseStrToEnumStr(QueryState) + "'";
            }
            dsScheduleInfoForDay.Tables[0].DefaultView.RowFilter = filter;
            gdCurrSchedule.DataSource = dsScheduleInfoForDay.Tables[0].DefaultView;

        }
        private void cbxScheduleType_SelectedIndexChanged(object sender, EventArgs e)
        {
            QueryTodaySchedule(cbxScheduleType.SelectedItem.ToString());
        }

        private void btnEditScheduleForDay_Click(object sender, EventArgs e)
        {
            if (gvCurrSchedule.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择会员的预约记录。");
                return;
            }

            DataRow drRow = gvCurrSchedule.GetDataRow(gvCurrSchedule.FocusedRowHandle);
            DBEntitySchedule scheduleInfo = new DBEntitySchedule(drRow);
            DBEntityCustomInfo customInfo = new DBEntityCustomInfo(drRow);
            DBEntityScheduleCustom scheduleCustomInfo = new DBEntityScheduleCustom(drRow);
            frmScheduleInfoAddCustom frmScheduleInfo = new frmScheduleInfoAddCustom(scheduleInfo, scheduleCustomInfo, customInfo);
            if (frmScheduleInfo.ShowDialog() == DialogResult.OK)
            {
                gvCurrSchedule.BeginUpdate();
                initTodaySchedule();
                QueryTodaySchedule(cbxScheduleType.SelectedItem.ToString());
                gvCurrSchedule.EndUpdate();
                
            }
        }

        private void btnDeleteScheduleForDay_Click(object sender, EventArgs e)
        {
            if (gvCurrSchedule.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择会员的预约记录。");
                return;
            }
            if (!MsgBox.MsgQue("是否要删除所选择的预约记录？"))
            {
                return;
            }

            DataRow drRow = gvCurrSchedule.GetDataRow(gvCurrSchedule.FocusedRowHandle);
            DBEntitySchedule scheduleInfo = new DBEntitySchedule(drRow);
            DBEntityScheduleCustom scheduleCustomInfo = new DBEntityScheduleCustom(drRow);

            string error = string.Empty;
            if (!BussinessProxyManager.Instance.DeleteScheduleCustom(scheduleInfo, scheduleCustomInfo, out error))
            {
                MsgBox.MsgInfo("删除预约记录错误：" + error);
                return;
            }
            gvCurrSchedule.BeginUpdate();
            initTodaySchedule();
            QueryTodaySchedule(cbxScheduleType.SelectedItem.ToString());
            gvCurrSchedule.EndUpdate();
        }

        private void btnAddScheduleForDay_Click(object sender, EventArgs e)
        {
            if (phoneCallList.CustomInfo == null)
            {
                MsgBox.MsgInfo("没有找到来电显示中的用户信息。");
                return;
            }

            frmScheduleInfoAddCustom frmSchduleAdd = new frmScheduleInfoAddCustom(null, null, phoneCallList.CustomInfo);
            if (frmSchduleAdd.ShowDialog() == DialogResult.OK)
            {
                gvCurrSchedule.BeginUpdate();
                initTodaySchedule();
                QueryTodaySchedule(cbxScheduleType.SelectedItem.ToString());
                gvCurrSchedule.EndUpdate();
            }
        }

        private void gvCurrSchedule_DoubleClick(object sender, EventArgs e)
        {
            if (gvCurrSchedule.FocusedRowHandle < 0)
            {
                return;
            }
            btnEditScheduleForDay_Click(sender, e);

        }
        #endregion

        private void SchedulingView_Load(object sender, EventArgs e)
        {
            initTodaySchedule();
            cbxScheduleType.SelectedIndex = 0;

            dateTimeCallStart.Value = DateTime.Now;
            cbxState.SelectedIndex = 0;
        }

        #region 提醒设置


        private void btnAddCall_Click(object sender, EventArgs e)
        {
            frmCallInfo callInfo = new frmCallInfo(null);
            if (callInfo.ShowDialog() == DialogResult.OK)
            {
                btnQueryCall_Click(sender, e);
            }
        }

        private void btnEditCall_Click(object sender, EventArgs e)
        {
            if (gvCall.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择正确的记录。");
                return;
            }
            string error = string.Empty;
            DBEntitySchedule scheduleInfo = new DBEntitySchedule(gvCall.GetDataRow(gvCall.FocusedRowHandle));
            frmCallInfo callInfo = new frmCallInfo(scheduleInfo);
            if (callInfo.ShowDialog() == DialogResult.OK)
            {
                btnQueryCall_Click(sender, e);
            }
        }

        private void btnDeleteCall_Click(object sender, EventArgs e)
        {
            if (gvCall.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择正确的记录。");
                return;
            }
            string error = string.Empty;
            DBEntitySchedule scheduleInfo = new DBEntitySchedule(gvCall.GetDataRow(gvCall.FocusedRowHandle));
            if (!BussinessProxyManager.Instance.DeleteScheduleCustom(scheduleInfo, null, out error))
            {
                MsgBox.MsgInfo("删除数据错误：" + error);
                return;
            }
            btnQueryCall_Click(sender, e);
        }

        private void btnQueryCall_Click(object sender, EventArgs e)
        {
            string filterState = "";
            if (!cbxState.SelectedItem.ToString().Equals("全部记录"))
            {
                filterState = DBEntitySchedule.ENUM_STATE.ChineseStrToEnumStr(cbxState.SelectedItem.ToString());
            }

        }

        #endregion

        

    }
}
